package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.common.DelStatus;
import com.youi.service.OrdergoodsService;
import com.youi.entity.Ordergoods;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 订单商品信息控制层
* @author Administrator
*/
@Controller
public class OrdergoodsController  extends HttpRequestManage{

		@Autowired
		private OrdergoodsService ordergoodsService;

		/**
		 *添加或编辑订单商品信息
		*/
		@ResponseBody
		@RequestMapping("addOrEditOrdergoods")
		public String addOrEditOrdergoods(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Ordergoods ordergoods=null;
			if (id!=null) {
				ordergoods=ordergoodsService.get(id);
			}else {
				ordergoods=new Ordergoods();
				ordergoods.setDelStatus(DelStatus.NODEL);
				ordergoods.setGmtCreated(new Date());
			}
			Long orderId=optLong("orderId",request);
			if (orderId!=null){
				ordergoods.setOrderId(orderId);
			}
			Long merchantId=optLong("merchantId",request);
			if (merchantId!=null){
				ordergoods.setMerchantId(merchantId);
			}
			Long goodsId=optLong("goodsId",request);
			if (goodsId!=null){
				ordergoods.setGoodsId(goodsId);
			}
			String goodsImg=optString("goodsImg",request);
			if (goodsImg!=null){
				ordergoods.setGoodsImg(goodsImg);
			}
			String name=optString("name",request);
			if (name!=null){
				ordergoods.setName(name);
			}
			String goodsNo=optString("goodsNo",request);
			if (goodsNo!=null){
				ordergoods.setGoodsNo(goodsNo);
			}
			String internationalNo=optString("internationalNo",request);
			if (internationalNo!=null){
				ordergoods.setInternationalNo(internationalNo);
			}
			String goodsType=optString("goodsType",request);
			if (goodsType!=null){
				ordergoods.setGoodsType(goodsType);
			}
			String specification=optString("specification",request);
			if (specification!=null){
				ordergoods.setSpecification(specification);
			}
			Double ecPrice=optDouble("ecPrice",request);
			if (ecPrice!=null){
				ordergoods.setEcPrice(ecPrice);
			}
			Double slaePrice=optDouble("slaePrice",request);
			if (slaePrice!=null){
				ordergoods.setSlaePrice(slaePrice);
			}
			Double originalPrice=optDouble("originalPrice",request);
			if (originalPrice!=null){
				ordergoods.setOriginalPrice(originalPrice);
			}
			Double costPrice=optDouble("costPrice",request);
			if (costPrice!=null){
				ordergoods.setCostPrice(costPrice);
			}
			Integer count=optInteger("count",request);
			if (count!=null){
				ordergoods.setCount(count);
			}
			Double amount=optDouble("amount",request);
			if (amount!=null){
				ordergoods.setAmount(amount);
			}
			Double weight=optDouble("weight",request);
			if (weight!=null){
				ordergoods.setWeight(weight);
			}
			ordergoods.setGmtModify(new Date());
			ordergoodsService.saveOrUpdate(ordergoods);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除订单商品信息
		*/
		@ResponseBody
		@RequestMapping("delOrdergoodsByIds")
		public String delOrdergoodsByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Ordergoods> ordergoodsList= ordergoodsService.getByParams(params);
			if (ordergoodsList==null||ordergoodsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Ordergoods ordergoods : ordergoodsList) {
				ordergoods.setDelStatus(DelStatus.DEL);
			}
			ordergoodsService.saveOrUpdateAll(ordergoodsList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * 分页查询订单商品信息
		*/
		@ResponseBody
		@RequestMapping("getOrdergoods")
		public String getOrdergoods(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Ordergoods> ordergoodsList=ordergoodsService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (ordergoodsList==null||ordergoodsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=ordergoodsService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, ordergoodsList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 *根据id查询订单商品信息
		*/
		@ResponseBody
		@RequestMapping("getOrdergoodsById")
		public String getOrdergoodsById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Ordergoods ordergoods=ordergoodsService.get(id);
			if (ordergoods==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",ordergoods);//jsonObject	json对象
		}

		/**
		 * 查询所有订单商品信息
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllOrdergoods")
		public String getAllOrdergoods(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Ordergoods> ordergoodsList= ordergoodsService.getByParams(null);
			if (ordergoodsList==null||ordergoodsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",ordergoodsList);//jsonObjectList	json对象集合
		}
}
